<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Gcode Viewer</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<!-- Modified from THREE.JS Gcode loader example-->
		<style>
			body {
				font-family: Monospace;
				background-color: #FFFFFF;
				margin: 0px;
				overflow: hidden;
			}
			#infotab{
				position:fixed;
				z-index:999;
				right:10px;
				bottom:0px;
				max-width:480px;
				word-break: break-all;
				color:white;
			}
		</style>
	</head>
	<body>
		<script src="script/Threejs/build/three.js"></script>
		<script src="script/Threejs/OrbitControls.js"></script>
		<script src="script/Threejs/GCodeLoader.js"></script>
		<script src="../../script/jquery.min.js"></script>
		<script src="../../script/ao_module.js"></script>
		<div id="infotab">
			<p id="filename"></p>
			<p id="filepath" style="display:none;"></p>
			<p id="displayfilepath"></p>
			<p id="filesize"></p>
		</div>
		<script>
			//Get file information from the hash info
			var files = ao_module_loadInputFiles();
			var file = "";
			if (files.length > 0){
				file = files[0];
				$("#filename").text(file.filename);
				$("#filepath").text("../../media?file=" + file.filepath);
				$("#displayfilepath").text(file.filepath);
				//Get filesize info
				$.ajax({
					url: "../../system/file_system/getProperties", 
					data: {path: file.filepath},
					success: function(data){
						var filesize = ao_module_utils.formatBytes(data.Filesize, 2);
						$("#filesize").text(filesize);
					}
				});
				
			}

			//ao module initiation
			ao_module_setWindowTitle("GCODEviewer - " + $("#filename").text().trim());

			var container;
			var camera, scene, renderer;

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );
				camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 10000 );
				camera.position.set(0,50,100);
				

				var controls = new THREE.OrbitControls( camera );
				controls.target = new THREE.Vector3(0, 20, 0);
				controls.update();
				scene = new THREE.Scene();
				
				//Setup the background color and the platform
				var backgroundcolor = new THREE.Color("#212121");
				scene.background = backgroundcolor;
				
				var loader = new THREE.GCodeLoader();
				loader.load( $("#filepath").text(), function ( object ) {
				    var box = new THREE.Box3().setFromObject( object );
                    const center = new THREE.Vector3();
                    box.getSize(center)
                    console.log(center)
					object.position.set(0,0,0);
					camera.position.set(center.x,50,center.z);
					controls.update();
					scene.add( object );
					
				} );
				
				renderer = new THREE.WebGLRenderer();
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				container.appendChild( renderer.domElement );
				window.addEventListener( 'resize', resize, false );
			}
			

			function resize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animate() {

				renderer.render( scene, camera );

				requestAnimationFrame( animate );

			}
		</script>

	</body>
</html>
